//Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using NUnit.Framework;

namespace Microsoft.Office.OneNote.UnitTests
{
	/// <summary>
	/// Tests the Notebook class.
	/// </summary>
	[TestFixture]
	public class NotebookTest
	{
		[SetUp]
		public void Init()
		{
			NotebookRoot root = OneNote.Application.NotebookRoot;

			// Let's create a new notebook:
			this.testNoteboook = TestUtils.CreateEmptyNotebook();
		}

		[TearDown]
		public void Dispose()
		{
			TestUtils.CloseAndDeleteNotebook(this.testNoteboook);
			this.testNoteboook = null;
		}

		[Test]
		public void NotebookColor()
		{
			Assert.IsTrue(this.testNoteboook.Color == Color.Empty);

			using (HierarchyTransaction notebookColorTransaction = new HierarchyTransaction(this.testNoteboook))
			{
				this.testNoteboook.Color = Color.FromArgb(255, 0, 255);
				notebookColorTransaction.Commit();

				Assert.IsTrue(this.testNoteboook.Color == Color.FromArgb(255, 0, 255));

				this.testNoteboook.Color = Color.Blue;
				notebookColorTransaction.Commit();

				// Color.Blue is a named color, and won't compare exactly after serialization:
				Assert.IsTrue(this.testNoteboook.Color == Color.FromArgb(Color.Blue.ToArgb()));
			}
		}

		[Test]
		public void RenameNotebook()
		{
			// Great!  Now let's rename it:
			using (HierarchyTransaction renameNotebookTransaction = new HierarchyTransaction(this.testNoteboook))
			{
				this.testNoteboook.Nickname = "Test Rename";
			}

			Assert.IsTrue(this.testNoteboook.Nickname == "Test Rename");
		}

		[Test]
		public void CanContain()
		{
			Assert.IsTrue(this.testNoteboook.CanContain(new SectionGroup()));
			Assert.IsTrue(this.testNoteboook.CanContain(new Section()));
			Assert.IsFalse(this.testNoteboook.CanContain(new Page()));
		}

		[Test]
		public void AddSectionGroup()
		{
			Assert.IsTrue(this.testNoteboook.GetChildCount() == 0);

			using (HierarchyTransaction createSectionGroup = new HierarchyTransaction(testNoteboook))
			{
				this.testNoteboook.AppendChild(new SectionGroup("Test Group"));
			}

			Assert.IsTrue(this.testNoteboook.GetChildCount() == 1);
		}

		[Test]
		public void DeleteSectionGroup()
		{
			Assert.IsTrue(this.testNoteboook.GetChildCount() == 0);

			using (HierarchyTransaction addAndDeleteSectionGroup = new HierarchyTransaction(testNoteboook))
			{
				this.testNoteboook.AppendChild(new SectionGroup("Test Group"));
				addAndDeleteSectionGroup.Commit();
				Assert.IsTrue(this.testNoteboook.GetChildCount() == 1);

				this.testNoteboook.DeleteChild(this.testNoteboook.GetChild(0));
			}

			Assert.IsTrue(this.testNoteboook.GetChildCount() == 0);
		}

		[Test]
		public void AddSection()
		{
			Assert.IsTrue(this.testNoteboook.GetChildCount() == 0);

			using (HierarchyTransaction createSection = new HierarchyTransaction(testNoteboook))
			{
				this.testNoteboook.AppendChild(new Section("Test Section"));
			}

			Assert.IsTrue(this.testNoteboook.GetChildCount() == 1);
		}

		[Test]
		public void DeleteSection()
		{
			Assert.IsTrue(this.testNoteboook.GetChildCount() == 0);

			using (HierarchyTransaction addAndDeleteSection = new HierarchyTransaction(testNoteboook))
			{
				this.testNoteboook.AppendChild(new Section("Test Section"));
				addAndDeleteSection.Commit();
				Assert.IsTrue(this.testNoteboook.GetChildCount() == 1);

				this.testNoteboook.DeleteChild(this.testNoteboook.GetChild(0));
			}

			Assert.IsTrue(this.testNoteboook.GetChildCount() == 0);
		}

		private Notebook testNoteboook = null;
	}
}